
#include "graphics/modelmanager.h"
#include "graphics/model.h"

namespace dw
{
namespace graphics
{

Model* ModelManager::loadModel(const std::string& filename)
{
	dwAssert(!filename.empty(), "Invalid parameter: filename");

	ModelNameMap::iterator it = _modelsByName.find(filename);

	if (it == _modelsByName.end())
	{
		Model* model = Model::fromFile(filename);

		_modelsByName.insert(std::make_pair(filename, model));
		
		return model;
	}
	else
	{
		return it->second;
	}
}

void ModelManager::unloadModel(const std::string& filename)
{
	if (filename.empty()) throw std::invalid_argument(__FUNCTION__);

	ModelIterator it = _modelsByName.find(filename);
	if (it != _modelsByName.end())
	{
		delete it->second;
		_modelsByName.erase(it);
	}
}

void ModelManager::unloadAll()
{
	for (ModelIterator it = _modelsByName.begin(); it != _modelsByName.end(); ++it)
		delete it->second;
	_modelsByName.clear();
}

bool ModelManager::isModelLoaded(const std::string& name)
{
	ModelIterator it = _modelsByName.find(name);
	if (it != _modelsByName.end())
		return true;
	else return false;
}

} // namespace graphics
} // namespace dw
